package com.example.android.ouactivity.utils;
/*
 * Created by kelion on 2016/11/20.
 */

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import android.widget.ImageView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class ImgLoader {

    private String mUrl;

    //创建Cache
    private LruCache<String ,Bitmap> mCache;

    public void showImageByAsyncTask(ImageView imageView,String url){
        Bitmap bitmap = getBitmapFromCache(url);
        if(bitmap==null){
            new NewsAsyncTask(imageView,url).execute(url);
        }else {
            imageView.setImageBitmap(bitmap);
        }
    }



    public Bitmap getBitmapFromURL(String urlString) {
        Bitmap bitmap;
        InputStream is;
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            is = new BufferedInputStream(connection.getInputStream());
            bitmap = BitmapFactory.decodeStream(is);
            connection.disconnect();
            return bitmap;
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        return null;
    }



    private class NewsAsyncTask extends AsyncTask<String,Void,Bitmap>{

        private ImageView mImageView;
        NewsAsyncTask(ImageView imageView, String url){
            mImageView = imageView;
            mUrl = url;
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            String url = strings[0];
            Bitmap bitmap = getBitmapFromURL(url);
            /**
             *	从网络获取图片
             */
            if(bitmap != null)
            {
                //把图片加入缓存
                addBitmapToCache(url, bitmap);
            }
            //将Result传给onPostExecute(Result)
            return bitmap;

        }


        @Override
        //在doInBackground(Params...)完成后，由UI线程调用，在这里处理Result。
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if (mImageView.getTag().equals(mUrl)&&bitmap != null){
                mImageView.setImageBitmap(bitmap);
            }
        }

    }


    public ImgLoader(){
        //获取最大可用缓存
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        mCache = new LruCache<String,Bitmap>(maxMemory/5){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                // 在每次存入缓存时调用
                return value.getByteCount();
            }
        };
    }


    /**
     * 从缓存中获取数据
     *
     * @param url
     * @return
     */
    public Bitmap getBitmapFromCache(String url)
    {
        return mCache.get(url);
    }



    /**
     * 增加到缓存
     *
     * @param url   图片的网站,也是图片的唯一标志
     * @param bitmap
     */
    public void addBitmapToCache(String url, Bitmap bitmap)
    {
        if(getBitmapFromCache(url) == null)
        {
            mCache.put(url, bitmap);
        }
    }



}
